﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communication = PSE.Deployment.TeamFoundation.Communication;
using PSE.Deployment.TeamFoundation.Communication;
using PSE.Deployment.TeamFoundation.Entity;
using PSE.Deployment.TeamFoundation.Entity.Enum;
using System.Threading;

namespace PSE.Deployment.TeamFoundation.Logic
{
    public class WorkItem
    {
        #region [ Events ]
        public event EventHandler<PSE.Deployment.TeamFoundation.Entity.TraceEventArgs> TraceEvent;
        #endregion [ Events ]

        #region Singleton
        private static WorkItem instance;
        protected WorkItem()
        {
            Communication.WorkItem.Instance.TraceEvent += (sender, e) => TraceEvent(sender, e);
        }
        public static WorkItem Instance
        {
            get
            {
                if (instance == null)
                {
                    Mutex mutex = new Mutex();
                    mutex.WaitOne();

                    if (instance == null)
                        instance = new WorkItem();

                    mutex.Close();
                }
                return instance;
            }
        }
        #endregion Singleton

        public List<PSE.Deployment.TeamFoundation.Entity.WorkItem> GetAllRevision(PSE.Deployment.TeamFoundation.Entity.TeamProject teamProject, PSE.Deployment.TeamFoundation.Entity.PackageIterationBranch packageIterationBranch)
        {
            return Communication.WorkItem.Instance.GetAllRevision(teamProject, packageIterationBranch);
        }

        public List<PSE.Deployment.TeamFoundation.Entity.WorkItem> GetAllRevision(PSE.Deployment.TeamFoundation.Entity.TeamProject teamProject, string versionLabel)
        {
            return Communication.WorkItem.Instance.GetAllRevision(teamProject, versionLabel);
        }

        public void OpenHotFix(PSE.Deployment.TeamFoundation.Entity.TeamProject teamProject, PSE.Deployment.TeamFoundation.Entity.PackageIterationBranch packageIterationBranch, string releaseGroupName, string systemId, string releaseVersion)
        {
            Communication.WorkItem.Instance.OpenHotFix(teamProject, packageIterationBranch, releaseGroupName, systemId, releaseVersion);
        }

        public int OpenServicePack(PSE.Deployment.TeamFoundation.Entity.TeamProject teamProject, PSE.Deployment.TeamFoundation.Entity.PackageIterationBranch packageIterationBranch, string releaseGroupName, string systemId, string releaseVersion)
        {
            return Communication.WorkItem.Instance.OpenServicePack(teamProject, packageIterationBranch, releaseGroupName, systemId, releaseVersion);
        }

        public string GenerateVersion(WorkItemType workItemType, string versionLabel, string teamProjectName, string packageIterationBranch)
        {
            return Communication.WorkItem.Instance.GenerateVersion(workItemType, versionLabel, teamProjectName, packageIterationBranch);
        }

        public List<PSE.Deployment.TeamFoundation.Entity.WorkItem> GetAllOpenedHotFixes()
        {
            return Communication.WorkItem.Instance.GetAllOpenedHotFixes();
        }

        public void ChangeWorkItemState(int workItemId, PSE.Deployment.TeamFoundation.Entity.WorkItemState state)
        {
            Communication.WorkItem.Instance.ChangeWorkItemField(workItemId, WorkItemField.State, Communication.WorkItem.Instance.GetEnumDescription(state));
        }

        public void ChangeWorkItemField(int workItemId, WorkItemField field, string value)
        {
            Communication.WorkItem.Instance.ChangeWorkItemField(workItemId, field, value);
        }

        public Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem GetWorkItem(PSE.Deployment.TeamFoundation.Entity.WorkItem workItem)
        {
            return Communication.WorkItem.Instance.GetWorkItem(workItem);
        }
    }
}
